package com.base.cn.platform.os.service.statistics.flow;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.statistics.flow.ZStatisticsTerminalUVCountDao;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.entity.statistics.flow.ZStatisticsTerminalUVCount;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 终端统计Biz
 *
 * @author s.li
 * @date 2019/04/29
 */
@Service
public class ZStatisticsTerminalUVCountBiz extends BaseBiz<ZStatisticsTerminalUVCount, ZStatisticsTerminalUVCountDao> {

    /**
     * 生成终端访问UV数据统计
     * @param terminalUVCountList
     */
    public synchronized void genTerminalUVCount(List<ZStatisticsTerminalUVCount> terminalUVCountList){
        if(ObjectUtils.isNotEmpty(terminalUVCountList)){
            String timeWhereSql = terminalUVCountList.stream().map(e-> "countTime='"+ DateUtils.format(e.getCountTime(),"yyyy-MM-dd")+"'").collect(Collectors.joining(" or "," 1=1 and (",")"));
            List<ZStatisticsTerminalUVCount> uniqueVisitorCountList = this.find(timeWhereSql,null,null);
            List<ZStatisticsTerminalUVCount> updateList = new ArrayList<>();
            List<ZStatisticsTerminalUVCount> addList = new ArrayList<>();
            if(ObjectUtils.isNotEmpty(uniqueVisitorCountList)){
                Map<String,ZStatisticsTerminalUVCount> oldUniqueVisitorMap = uniqueVisitorCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(),"yyyy-MM-dd"), e->e));
                Map<String,ZStatisticsTerminalUVCount> newUniqueVisitorMap = terminalUVCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(),"yyyy-MM-dd"), e->e));
                newUniqueVisitorMap.forEach((k,v)->{
                    ZStatisticsTerminalUVCount terminalUVCount = oldUniqueVisitorMap.get(k);
                    if(terminalUVCount !=null){
                        terminalUVCount.setWindowCount(terminalUVCount.getWindowCount() + v.getWindowCount());
                        terminalUVCount.setMacCount(terminalUVCount.getMacCount() + v.getMacCount());
                        terminalUVCount.setAndCount(terminalUVCount.getAndCount() + v.getAndCount());
                        terminalUVCount.setIosCount(terminalUVCount.getIosCount() + v.getIosCount());
                        terminalUVCount.setOtherCount(terminalUVCount.getOtherCount() + v.getOtherCount());
                        terminalUVCount.setUvCount(terminalUVCount.getUvCount() + v.getUvCount());
                        updateList.add(terminalUVCount);
                    }else{
                        addList.add(v);
                    }
                });
            }else{
                addList.addAll(terminalUVCountList);
            }
            if(ObjectUtils.isNotEmpty(addList)){
                this.batchSave(addList);
            }
            if(ObjectUtils.isNotEmpty(updateList)){
                this.updateBatch(updateList);
            }
        }
    }

    /**
     * 获取设置访问分布用户数据统计
     * @param dateQueryCondition
     * @return ZStatisticsTerminalUVCount
     */
    public ZStatisticsTerminalUVCount queryTerminalUVCountData(StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeSql = StatisticsDateQueryCondition.getTimeSql(dateList,"yyyy-MM-dd");
        List<ZStatisticsTerminalUVCount> terminalUVCountList = this.find(" countTime in "+timeSql,null,null);
        if(ObjectUtils.isNotEmpty(terminalUVCountList)){
            ZStatisticsTerminalUVCount terminalUVCount = new ZStatisticsTerminalUVCount();
            terminalUVCount.setWindowCount(terminalUVCountList.stream().mapToInt(ZStatisticsTerminalUVCount::getWindowCount).sum());
            terminalUVCount.setMacCount(terminalUVCountList.stream().mapToInt(ZStatisticsTerminalUVCount::getMacCount).sum());
            terminalUVCount.setAndCount(terminalUVCountList.stream().mapToInt(ZStatisticsTerminalUVCount::getAndCount).sum());
            terminalUVCount.setIosCount(terminalUVCountList.stream().mapToInt(ZStatisticsTerminalUVCount::getIosCount).sum());
            terminalUVCount.setOtherCount(terminalUVCountList.stream().mapToInt(ZStatisticsTerminalUVCount::getOtherCount).sum());
            terminalUVCount.setUvCount(terminalUVCountList.stream().mapToInt(ZStatisticsTerminalUVCount::getUvCount).sum());
            return terminalUVCount;
        }else{
            return new ZStatisticsTerminalUVCount();
        }
    }
}
